//
//  RecommendViewController.m
//  MXClassroom
//
//  Created by 你个LB on 16/9/8.
//  Copyright © 2016年 你个LB. All rights reserved.
//

#import "RecommendViewController.h"

@interface RecommendViewController ()

@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    
    [self initDataSource];
    
    [self initTableView];
}

// 单元格数据
- (void)initDataSource{
    
    NSMutableArray * models = [NSMutableArray array];
    
    NSArray * data = @[@{@"imageName":@"妙心直播间100px.png",
                         @"title":@"次第修学班",
                         @"introduction":@"这是一个第次修学简介，这是一个简无上甚深微妙法百千万劫难遭遇我今见问得受持头解如来",
                         @"person_Number":@"12345"},
                       @{@"imageName":@"次第修学班级100px.png",
                         @"title":@"妙心直播间",
                         @"introduction":@"这是二个第次修学简介，这是一个简无上甚深微妙法百千万劫难遭遇我今见问得受持头解如来",
                         @"person_Number":@"54321"}];
    
    for (int i = 0; i < 2; i ++) {
        RecommendModel * recommend = [[RecommendModel alloc] initWithContentDic:data[i]];
        [models addObject:recommend];
    }
    _dataList = models;
}

// 创建表试图
- (void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height - 64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    // 添加头试图
    [self initTableHeadView];
}

- (void)initTableHeadView{
    LBPageTurnView * pageTurnView = [[LBPageTurnView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 150)];
    
    pageTurnView.pageControlFrame = CGRectMake((kScreen_width - 51) / 2, pageTurnView.bottom - 20, 51, 20);
    pageTurnView.imageNames = @[@"index_banner@2x.png", @"index_banner@2x 2.png", @"index_banner@2x 3.png", @"index_banner@2x 4.png"];
    pageTurnView.pageTurnTime = 3.0;
    _tableView.tableHeaderView = pageTurnView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return _dataList.count;
    } else {
        return 1;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        RecommendTableViewCell * cell = [RecommendTableViewCell cellWithTableView:tableView];
        cell.recommend = _dataList[indexPath.row];
        return cell;
    } else {
        static NSString * reuseIdentifier = @"btnCellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (cell == nil) {
            // 创建cell
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            
            int space = (kScreen_width - 4 * 50 - 2 * 17) / 3;
            
            NSArray * titleArr = @[@"梵音妙乐", @"听故事", @"热门讨论", @"佛学科学"];
            
            NSArray * imageArr = @[@"icon_main_recommend_music_normal.png", @"icon_main_recommend_story_normal.png", @"icon_main_recommend_topic_normal.png", @"icon_main_recommend_science_normal.png"];
            
            NSArray * highlightedImageArr = @[@"icon_main_recommend_music_pressed.png", @"icon_main_recommend_story_pressed.png", @"icon_main_recommend_topic_pressed.png", @"icon_main_recommend_science_pressed.png"];
            
            for (int i = 0; i < 4; i ++) {
                UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(17 + i * 50 + i * space, 8, 50, 50);
                [button setImage:[UIImage imageNamed:imageArr[i]] forState:UIControlStateNormal];
                [button setImage:[UIImage imageNamed:highlightedImageArr[i]] forState:UIControlStateHighlighted];
                [cell addSubview:button];
                
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(button.left, button.bottom, 50, 20)];
                label.textColor = kColor_text_color;
                label.font = [UIFont systemFontOfSize:10];
                label.textAlignment = NSTextAlignmentCenter;
                label.text = titleArr[i];
                [cell addSubview:label];
            }
        }
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50 + 32;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
